<template>
  <div>
    <article class="innerPage bookList">
      <header class="breadcrumb">
        <x-breadcrumb :path="$route.path" :title="$route.meta.breadcrumb" :startIndex="$route.meta.startIndex">
        </x-breadcrumb>
      </header>
      <section class="graphical">
        <SearchCondition
          :formItemList="formItemList"
          :emitSearch="emitSearch"
          :resetSearch="resetSearch"
          @search="search"
          ref="mychild"
        />
      </section>
      <div class="decorate"></div>
      <section class="graphical">
        <div class="btn_addclass">
          <el-button icon="el-icon-circle-plus-outline" class="btn-green icon-fontSize" v-alive @click="addbtn"
            >添加类型</el-button
          >
          <div></div>
        </div>
        <div>
          <el-table
            :data="tableData"
            class="tableStyle"
            :header-cell-style="$headerCellStyle"
            :border="$isborder"
            stripe
            size="mini"
          >
            <el-table-column prop="key" label="序号" label-class-name="indexColumn" :width="$widtha" align="center" fixed="left"> </el-table-column>
            <el-table-column prop="type_name" :min-width="$widthb" label="类型名" align="left"> </el-table-column>
            <el-table-column prop="create_time" :min-width="$widthb" label="添加时间" align="left"> </el-table-column>
            <el-table-column label="操作" align="left" width="180" :header-align="$heaerOpt">
              <template slot-scope="scope">
                <div class="btnFrom-box">
                  <div class="details-box" v-alive @click="ischang(scope.row.id, scope.row.type_name)">修改</div>
                  <div v-alive @click="activiDel(scope.row.id)" class="delete-box"><span></span> 删除</div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </section>
      <footer class="pagination">
        <div>
          <el-pagination
            background
            prev-text="上一页"
            next-text="下一页"
            layout="prev, pager, next, jumper"
            :total="listTotal"
            :page-size="10"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage1"
          >
          </el-pagination>
          
        </div>
        <p>
          共
          <span>{{ listTotal || 0 }}</span>
          数据
        </p>
      </footer>
    </article>
    <div>
      <Ddlmoadl ref="isdel" @deletesuccess="deletesuccess"></Ddlmoadl>
    </div>
    <div>
      <Dialog ref="isdialog" @showType="showType"> </Dialog>
    </div>
  </div>
</template>

<script>
import Ddlmoadl from "../../../components/Deletebtn/Deletetk.vue"
import Dialog from "../../../components/addDialog/index.vue"
export default {
  name: "AdminIndex",
  components: {
    Ddlmoadl,
    Dialog,
  },
  data() {
    return {
      formItemList: [
        {
          label: "关键词",
          type: "input",
          param: "keywords",
        },
      ],
      searchList: {
        keywords: "",
      },
      emitSearch: false,
      resetSearch: true,
      tableData: [], //数据
      page: 1,
      listTotal: 0,
      currentPage1: 1,
      dialogVisible: false,
      typetext: "",
      btntype: "", //btn类型
      typeid: "", //数据id
      title: "",
      limit: 10, //
    }
  },

  mounted() {},

  activated() {
    if (this.$route.params.action === "refresh" || this.$isFirstInPage.get()) {
      this.page = 1
      this.listTotal = 0
      this.currentPage1 = 1
      this.tableData = []
      this.getList()
    }
  },
  methods: {
    //查询
    search(params) {
      this.searchList = params
      this.page = 1
      this.getList()
    },
    libFun() {
      this.getList()
    },
    //删除
    activiDel(id) {
      this.typeid = id
      this.$refs.isdel.delshow(id)
    },
    async deletesuccess() {
      try {
        let res = await this.request({
          url: "/admin/resourceTotalType/del",
          method: "post",
          loading: true,
          data: {
            id: this.typeid,
          },
        })
        if (res) {
          this.$message.success("删除成功")
          let that = this
          if (this.page >= 2) {
            if (this.tableData.length == 1) {
              this.page = this.page - 1
            }
          }
          setTimeout(() => {
            that.getList()
          }, 2000)
        }
      } catch (err) {
        this.errorHandler(err)
      }
    },
    //分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getList()
    },
    async getList() {
      try {
        let res = await this.request({
          url: "/admin/resourceTotalType/list",
          method: "get",
          loading: true,
          data: {
            page: this.page,
            limit: this.limit,
            ...this.searchList,
          },
        })
        if (res) {
          this.tableData = res.content.data
          this.listTotal = res.content.total
          this.currentPage1 = res.content.current_page
        }
      } catch (err) {
        let error = err.message.split("/")
        if (error[0] == 203) {
          this.tableData = []
          this.listTotal = 0
        }
        this.errorHandler(err)
      }
    },
    //新增
    async addType() {
      try {
        let res = await this.request({
          url: "/admin/resourceTotalType/add",
          method: "post",
          loading: true,
          data: {
            type_name: this.typetext,
          },
        })
        if (res) {
          this.$refs.isdialog.showdialog2()
          this.$message.success(res.msg)
          let that = this
          setTimeout(() => {
            that.getList()
          }, 2000)
        }
      } catch (err) {
        this.errorHandler(err)
      }
    },
    //增加
    addbtn() {
      this.dialogVisible = true
      this.btntype = "add"
      this.typetext = ""
      this.title = "添加类型"
      let title2 = "请输入类型"
      let labl = "类型"
      this.$refs.isdialog.showdialog(this.title, this.btntype, this.typetext, labl, title2)
    },
    //修改
    ischang(id, name) {
      this.typetext = ""
      this.dialogVisible = true
      this.btntype = "chang"
      this.typeid = id
      this.typetext = name
      this.title = "修改类型"
      let title2 = "请输入类型"
      let labl = "类型"
      this.$refs.isdialog.showdialog(this.title, this.btntype, this.typetext, labl, title2)
    },
    //
    async changType() {
      try {
        let res = await this.request({
          url: "/admin/resourceTotalType/change",
          method: "post",
          loading: true,
          data: {
            id: this.typeid,
            type_name: this.typetext,
          },
        })
        if (res) {
          this.$refs.isdialog.showdialog2()
          this.$message.success(res.msg)
          let that = this
          setTimeout(() => {
            that.getList()
          }, 2000)
        }
      } catch (err) {
        this.errorHandler(err)
      }
    },
    //根据返回的值判断是否修改或是添加
    showType(type, text) {
      console.log(type, "type")
      console.log(text, "text")
      if (type == "add") {
        this.typetext = text
        this.addType()
      } else {
        this.typetext = text
        this.changType()
      }
    },
  },
}
</script>

<style lang="scss" scoped>
@import "./style.scss";
</style>
